﻿using SHKF.B2C.YouZanYun.Model.Inter.Category;
using SHKF.B2C.YouZanYun.Model.Inter.Image;
using SHKF.B2C.YouZanYun.Model.Inter.MaterialCategory;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WQDSkin.Forms.Controls; 

namespace SHKF.B2C.Mannage.AdminUI.View.CenterInfo.Product
{
    /// <summary>
    /// frmItemAddYZY.xaml 的交互逻辑
    /// </summary>
    public partial class frmItemAddYZY : Window
    {
        public string authority_id = "";

        public frmItemAddYZY()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            GL.HttpClientYzy.authority_id = this.authority_id;

            //加载分组
            LoadImageCategory();

            LoadItemCategory1();
        }

        private async void LoadImageCategory()
        {
            this.tvCategory.Items.Clear();
             
            //未分组
            MaterialCategoryResult result = await GL.HttpClientYzy.GetMaterialCategory(2); 
            if (result.data != null && result.data.Count > 0)
            {
                WQDTreeViewItem item2 = new WQDTreeViewItem();

                MaterialCategoryData data = result.data[0];
                item2.DataContext = data;
                item2.Header = result.data[0].category_name + $"({data.count})"; 
                tvCategory.Items.Add(item2);
            }

            //自定义
            result = await GL.HttpClientYzy.GetMaterialCategory(1);
            if (result.data != null && result.data.Count > 0)
            { 
                WQDTreeViewItem item1 = new WQDTreeViewItem(); 
                item1.Header = "自定义分组";
                tvCategory.Items.Add(item1);

                foreach (MaterialCategoryData data in result.data)
                {
                    WQDTreeViewItem item3 = new WQDTreeViewItem();
                    item3.DataContext = data;
                    item3.Header = data.category_name + $"({data.count})";
                    item1.Items.Add(item3);
                }

                item1.IsExpanded = true;
            }

      
        }
         
        private async void tvCategory_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var data = this.tvCategory.SelectData;
            if (data != null)
            {
                MaterialCategoryData data1 = (MaterialCategoryData)data;

                //加载图片
                long category_id = data1.category_id;

                ImageResult result = await GL.HttpClientYzy.GetMaterialImageList(category_id);
                if (result.Items != null)
                {
                    this.dgvImage.ItemsSource = result.Items;
                }
            }
        }
         
        private void tvLevel1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView tv = (TreeView)sender;
            CategoryData data = (CategoryData)tv.SelectedValue;
            LoadItemCategory(this.tvLevel2, data.id);
        }

        private void tvLevel2_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView tv = (TreeView)sender;
            if( tv.SelectedValue != null)
            {
                CategoryData data = (CategoryData)tv.SelectedValue;
                LoadItemCategory(this.tvLevel3, data.id);
            } 
        }

        private void tvLevel3_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView tv = (TreeView)sender;
            if (tv.SelectedValue != null)
            {
                CategoryData data = (CategoryData)tv.SelectedValue;
                LoadItemCategory(this.tvLevel4, data.id);
            } 
        }
 
        private async void LoadItemCategory(TreeView tv, long id)
        {
            CategoryRequest category = await GL.HttpClientYzy.GetCategoryChildren(id);
            if (category.data != null)
            {
                tv.ItemsSource = category.data;
            }
            else
            {
                MessageBox.Show(category.message);
            }
        }

        /// <summary>
        /// 加载类目 
        /// </summary>
        private async void LoadItemCategory1()
        {
            CategoryRequest category1 = await GL.HttpClientYzy.GetCategoryChildren(0);
            if (category1.data != null)
            {
                foreach (CategoryData item in category1.data)
                {
                    CategoryRequest category2 = await GL.HttpClientYzy.GetCategoryChildren(item.id);
                    item.data = category2.data;
                }

                this.tvLevel1.ItemsSource = category1.data;
            }
            else
            {
                MessageBox.Show(category1.message);
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgvImage.SelectedItems == null || this.dgvImage.SelectedItems.Count < 3)
            {
                MessageBox.Show("请选择三张图片"); 
                return;
            }

            if (leaf_category_id == 0)
            {
                MessageBox.Show("请选择类目"); 
                return;
            }

            foreach (ImageItem imageData in this.dgvImage.SelectedItems)
            {
                this.ImageID.Add(imageData.media_id);
            }

            if (this.tvLevel3.SelectedValue != null)
            {
                CategoryData data = (CategoryData)tvLevel3.SelectedValue;
                _leaf_category_id = data.id; 
            }

            this.DialogResult = true;
            this.Close();

        }

        private List<long> ImageID = new List<long>();
        public List<long> media_id
        {
            get
            {
                return this.ImageID;
            }
        }

        private long _leaf_category_id = 0;
        /// <summary>
        /// 类目
        /// </summary>
        public long leaf_category_id
        {
            get
            {
                if (this.tvLevel1.SelectedValue != null)
                {
                    CategoryData data = (CategoryData)tvLevel1.SelectedValue;
                    _leaf_category_id = data.id;
                }

                if (this.tvLevel2.SelectedValue != null)
                {
                    CategoryData data = (CategoryData)tvLevel2.SelectedValue;
                    _leaf_category_id = data.id;
                }
                if (this.tvLevel3.SelectedValue != null)
                {
                    CategoryData data = (CategoryData)tvLevel3.SelectedValue;
                    _leaf_category_id = data.id;
                }

                if (this.tvLevel4.SelectedValue != null)
                {
                    CategoryData data = (CategoryData)tvLevel4.SelectedValue;
                    _leaf_category_id = data.id;
                }

                return _leaf_category_id;
            }
        }
 
    }
}
